//
//  WJVisitorView.swift
//  WJWEIBO
//
//  Created by 文杰 许                       on 2018/6/28.
//  Copyright © 2018年 文杰 许                      . All rights reserved.
//

import UIKit

class WJVisitorView: UIView
{
    /// 注册按钮
    lazy var registerBtn: UIButton = 
    {
        let btn = UIButton(title: "注册", normalColor: UIColor.orange, highlightedColor: UIColor.darkGray)
        btn.backgroundColor = UIColor.white
        btn.size = CGSize(width: 60, height: 30)
        btn.left = self.nothingImage.left + 15
        btn.top = self.tipLabel.bottom + 5
        return btn
    }()
    /// 登录按钮
    lazy var loginBtn: UIButton =
    {
        let btn = UIButton(title: "登录", normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        btn.backgroundColor = UIColor.white
        btn.size = CGSize(width: 60, height: 30)
        btn.right = self.nothingImage.right - 15
        btn.top = self.tipLabel.bottom + 5
        return btn
    }()
    
    /// 方可视图信息
    var visitorInfo : [String: String]?
    {
        didSet
        {
            // 使用guard let 守护
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"]
                else
            {
                return
            }
            
            tipLabel.text = message
            tipLabel.sizeToFit()
            
            if imageName == ""
            {
                startAnimation()
                return
            }
            
            nothingImage.image = UIImage(named: imageName)
        }
    }
    
    /// 构造函数
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        backgroundColor = UIColor.lightGray
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func startAnimation()
    {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2 * Double.pi
        ani.repeatCount = MAXFLOAT
        ani.duration = 15
        // 动画完成后或离开动画页面，不删除该动画
        // 设置连续动画很有用，避免跳到其他页面或退到后台，再进入有动画的页面时，动画停止的问题
        ani.isRemovedOnCompletion = false
        
        nothingImage.layer .add(ani, forKey: nil)
    }
    
    /// 空白页图片
    fileprivate lazy var nothingImage : UIImageView =
    {
        let imageV = UIImageView(image: #imageLiteral(resourceName: "pic_nothing"))
        imageV.centerX = self.centerX
        imageV.centerY = self.centerY - 50
        return imageV
    }()
    
    /// 空白页文字描述
    fileprivate lazy var tipLabel: UILabel =
    {
        let lab = UILabel(frame: CGRect(x: 0, y: 0, width: self.nothingImage.width, height: 50))
        lab.centerX = self.centerX
        lab.text = "这里什么都没有哦~"
        lab.textColor = UIColor.white
        lab.textAlignment = .center
        lab.numberOfLines = 0
        lab.font = UIFont.systemFont(ofSize: 16)
        lab.top = self.nothingImage.bottom + 5
        lab.left = self.nothingImage.left
        return lab
    }()
}

extension WJVisitorView
{
    fileprivate func setupUI()
    {
        // 添加控件
        addSubview(nothingImage)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
    }
}
